var table;
$(function(){
    table=$("#annualPara").DataTable({
        "ajax": {
            url:"../../admin/annualpara/Get",
            type:"POST",
            data:function(d){
            },beforeSend: function () {

            },error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("载入数据出错");
            }
        },
        "scrollX": 1120,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": true,
        "bAutoWidth": false,
        "bInfo": true,//页脚信息
        "bFilter": false,//禁用搜索
        "bSort":false,//禁用排序
        "aLengthMenu": [2,5,10],
        "sPaginationType":'full_numbers',
        "columns": [
            {"data":"id"},
            { "data": "annual_name" },
            { "data": "start_date" },
            { "data": "end_date" },
            { "data": "id" },
        ],
        "aoColumnDefs": [
            {
                "aTargets": [ 4 ],
                "mRender": function ( data, type, full ) {
                    return '<a onclick="editPara(\''+full.id+'\',\''+full.annual_name+'\',\''+full.start_date+'\',\''+full.end_date+'\')">修改</a>' +
                        '<a onclick="delPara(\''+full.id+'\')">删除</a>';
                }
            },

        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            $("td:first", nRow).html(iDisplayIndex + 1);
            return nRow;
        },
        "oLanguage": {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 条",
            "sZeroRecords": "没有匹配结果",
            "sInfo": " _START_ 至 _END_ 项结果，共 _TOTAL_ 项",//显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项
            "sInfoEmpty": "",//显示第 0 至 0 项结果，共 0 项
            "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
            "sInfoPostFix": "",
            "sSearch": "搜索：",
            "sUrl": "",
            "sEmptyTable": "没有数据呀!",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            }
        },
    });
})

new SMValidator('#annualParaForm');

function addPara(){
    $("#id").val("");
    $("#annual_name").val("");
    $("#start_date").val("");
    $("#end_date").val("");
    $("#annualParaModel").modal("show")
}

function modal_save(){
    if($("#start_date").val()!=""&&$("#end_date").val()!="")
    {
        if($("#start_date").val()>=$("#end_date").val()){
            Lobibox.notify('error', {
                msg: "开始时间大于结束时间",
                sound: false
            });
        }
        else {
            if(SMValidator.validate('#annualParaForm')){
                $.ajax({
                    url: "../../admin/annualpara",
                    type: "POST",
                    dataType: "json",
                    data: $("#annualParaForm").serialize(),
                    beforeSend: function () {
                    },
                    success: function (data) {
                        if(data == true){
                            var msg = "";
                            if($("#id").val()){
                                msg = "修改成功"
                            }else {
                                msg = "新增成功"
                            }
                            Lobibox.notify('success', {
                                msg: msg,
                                sound: false
                            });
                            $("#annualParaModel").modal("hide")
                            table.draw();
                        }else if (data == -1){
                            Lobibox.notify('error', {
                                msg: "年检参数设置无效",
                                sound: false
                            });
                        } else {
                            console.log(data)
                        }
                    }, error: function () {
                    }
                });
            }
        }
    }
}

function editPara(id,annual_name,start_date,end_date){
    $("#id").val(id);
    $("#annual_name").val(annual_name);
    $("#start_date").val(start_date);
    $("#end_date").val(end_date);
    $("#annualParaModel").modal("show")
}

function delPara(id){
    Lobibox.confirm({
        msg: "确认要删除此年检设置?",
        title:"删除年检设置",
        iconClass: false,
        callback: function ($this, type, ev) {
            if(type==='yes'){
                $.ajax({
                    url: "../../admin/annualpara/"+id+"?_csrf="+$("input[name=_csrf]").val(),
                    type: "DELETE",
                    dataType: "json",
                    success: function (data) {
                        if(data){
                            table.draw();
                            Lobibox.notify('success', {
                                msg: '删除成功',
                                sound: false
                            });
                        }
                        else {
                            console.log(id);
                            Lobibox.notify('error', {
                                msg: '删除失败',
                                sound: false
                            });
                        }

                    },
                    error: function () {

                    }
                });
            }
        }
    });
}